class MyBlockingQueue{
    private int[] data=null;
    private int capacity;//数组容量
    private int head=0;//指向队头
    private int tail=0;//指向队尾
    private int size=0;//队列中元素数量
    public MyBlockingQueue(int capacity){
        data=new int[capacity];
    }
    public void put(int elem){
        synchronized(this){
            while (size>=data.length){
                //这里需要阻塞
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            data[tail]=elem;
            tail++;
            if(tail>=data.length){
                tail=0;
            }
            size++;
            this.notify();
        }

    }
    public int take() throws InterruptedException {
        synchronized (this){
            while (size==0){
                //这里需要阻塞
                this.wait();
            }
            int ret=data[head];
            head++;
            if(head>=data.length){
                head=0;
            }
            size--;
            this.notify();
            return ret;
        }
    }
}

public class Demo2 {
    public static void main(String[] args) {

    }
}
